﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using DevExpress.Utils;
using BCL.BaseLib;
using BCL.BaseLib.Basics;
using BCL.Tool;

namespace BCL.Component
{
    public partial class Navbar : BaseFunctionControl
    {
        List<FunctionEntity> fucs;
        public Navbar(List<FunctionEntity> _fucs)
        {
            InitializeComponent();
            this.fucs = _fucs;
        }

        private event FunctionItem_Click item_Click;
        public event FunctionItem_Click Item_Click
        {
            add
            {
                item_Click += value;
            }
            remove
            {
                item_Click -= value;
            }
        }

        private void UserControl3_Load(object sender, EventArgs e)
        {
            foreach (var item in fucs)
            {
                if (!item.IsCreate)
                    continue;
                if ((item.ItemType & FunctionItemType.Group)!=0)
                {
                    NavBarGroup gg = new NavBarGroup();
                    gg.Caption = item.Title.Language();
                    gg.Tag = item;
                    this.navBarControl1.Groups.Add(gg);
                    if (item.Items != null)
                        foreach (var fe in item.Items)
                            if(item.IsCreate)
                                AddItem(gg, fe);
                    gg.LargeImage = item.Image;
                    gg.LargeImageSize = new Size(24,24);
                    if ((item.ItemType & FunctionItemType.Item) != 0)
                    {
                      
                    }
                }
            }
        }

        private void AddItem(NavBarGroup group, FunctionEntity fe)
        {
            NavBarItem gi = new NavBarItem();
            if (fe.Items != null)
            { 
              
            }
            
            gi.Tag = fe;
            gi.Caption = fe.Title.Language();
            if(fe.Tip!=null)
                AddTip(gi, fe.Tip.Title.Language(), fe.Tip.Text.Language());
            gi.LinkClicked += gi_LinkClicked;
            gi.SmallImage = fe.Image;
            gi.SmallImageSize = new System.Drawing.Size(20,20);
            gi.Enabled = fe.Enabled;
            group.ItemLinks.Add(gi);
            navBarControl1.Items.Add(gi);
        }

        void gi_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (item_Click != null)
                item_Click(e.Link.Item.Tag as FunctionEntity);
        }

        private void AddTip(NavBarItem bar, string title, string text)
        {
            ToolTipTitleItem tip = new ToolTipTitleItem { Text = text, LeftIndent = 6 };
            ToolTipTitleItem tiptitle = new ToolTipTitleItem { Text = title };
            SuperToolTip stip = new SuperToolTip();
            stip.Items.Add(tiptitle);
            stip.Items.Add(tip);
            bar.SuperTip = stip;
        }

        
    }

    public delegate void FunctionItem_Click(FunctionEntity fe);
}
